/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.util.List;
import controller.Basket;
import controller.StoreHouse;
import javax.inject.Named;
import javax.enterprise.context.ConversationScoped;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.enterprise.context.Conversation;
import javax.inject.Inject;
import model.Gnome;

/**
 *This class is used to handle to file market.xhtml
 * @author Guillaume
 */
@Named(value = "marketManager")
@ConversationScoped
public class MarketManager implements Serializable {

    @EJB
    private StoreHouse storeHouse;
    @Inject
    private Basket basket;
    @Inject
    private Conversation conversation;
    private Integer numberOfWantedGnome;
    private String typeGnome;

    private void startConversation() {
        if (conversation.isTransient()) {
            conversation.begin();
        }
    }

    public Conversation getConversation() {
        return conversation;
    }

    /** 
     * Creates a new instance of MarketManager 
     */
    public MarketManager() {
    }

    /**
     * To set the number of wanted gnomes for the basket
     */
    public void setNumberOfWantedGnome(Integer nbr) {
        numberOfWantedGnome = nbr;
    }
    /**
     * To get the number of wanted gnomes
     * @return return the number of wanted gnomes
     */
    public Integer getNumberOfWantedGnome() {
        return numberOfWantedGnome;
    }
    /**
     * This function is used when a user wants to add gnomes in his basket
     * @param str type of gnome
     */
    public void putWantedGnomeInTheBasket(String str) {
        basket.addItemsToTheBasket(storeHouse.getGnomeTypeX(str), numberOfWantedGnome);
        numberOfWantedGnome = 0;
    }
    /**
     *  This function is called to obtain a list of all gnomes stored in database
     * @return 
     */
    public List<Gnome> getListOfAllGnome() {
        startConversation();
        List<Gnome> list = storeHouse.getListOfAllGnome();
        return list;
    }
}
